package com.landerer.math;

import java.math.BigDecimal;
import java.math.MathContext;

public final class Line {

	private Point a;
	private Point b;

	public void setBeginning(double x, double y) {
		this.a = new Point.Builder(x, y).build();
	}

	public void setEnding(double x, double y) {
		this.b = new Point.Builder(x, y).build();
	}

	public double[] getBeginning() {

		return new double[] { this.a.getX(), this.a.getY() };
	}

	public double[] getEnding() {

		return new double[] { this.b.getX(), this.b.getY() };
	}

	public double[][] getPoints() {

		return new double[][] { this.getBeginning(), this.getEnding() };
	}

	public double getLength() {

		/** PREPARE **/

		if (a == null || b == null) {
			throw new RuntimeException("The line in not properly specified!");
		}
		BigDecimal lineA = BigDecimal.ZERO;
		BigDecimal lineB = BigDecimal.ZERO;
		BigDecimal lineC = BigDecimal.ZERO;

		/** EXECUTE **/

		// calculate lineA
		if (this.a.getX() < this.b.getX()) {
			lineA = new BigDecimal(this.b.getX() - this.a.getX());
		} else {
			lineA = new BigDecimal(this.a.getX() - this.b.getX());
		}

		// calculate lineB
		if (this.a.getY() < this.b.getY()) {
			lineB = new BigDecimal(this.b.getY() - this.a.getY());
		} else {
			lineB = new BigDecimal(this.a.getY() - this.b.getY());
		}

		// calculate lineC
		lineC = lineC.add(lineA.pow(2, MathContext.DECIMAL128), MathContext.DECIMAL128);
		lineC = lineC.add(lineB.pow(2, MathContext.DECIMAL128), MathContext.DECIMAL128);
		lineC = new BigDecimal(Math.sqrt(lineC.doubleValue()), MathContext.DECIMAL128);

		/** CONCLUDE **/
		return lineC.doubleValue();
	}
}
